package com.Lishuai.Control.Imp;



import com.Lishuai.Mapper.BookMapper;
import com.Lishuai.Pojo.Book;
import com.Lishuai.Pojo.BuyRequest;
import com.Lishuai.Pojo.User;
import com.Lishuai.Servce.UserServce;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import java.io.File;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/users")
public class UserControllerImp {

    @Autowired
    private UserServce userService;

    @PostMapping("/login")
    public boolean login(@RequestBody User user) {
        return userService.login(user);
    }

    @PostMapping("/register")
    public Integer register(@RequestBody User user) {
        return userService.addUser(user);
    }

    @PostMapping("/changeHeadImg/{id}")
    public String changeHeadImg(@PathVariable int id, @RequestParam("filePic") MultipartFile[] filePic) throws Exception {
        return userService.changeHeadImg(id, filePic);
    }

    @GetMapping("/books")
    public List<Book> selectBooks(@RequestBody Book book) {
        return userService.sekect(book);
    }

    @GetMapping("/allBooks")
    public List<Book> findAllBooks() {
        return userService.findAll();
    }

    @PostMapping("/buyBook")
    public Integer buyBook(@RequestBody BuyRequest buyRequest) {
        return userService.buyBook(buyRequest.getUser(),buyRequest.getBook());
    }
    @GetMapping("/getImage/{id}")
    public String getImage(@PathVariable("imageId") int id) {
        return userService.getImage(id);
    }
    @PostMapping("/recharge/{id}/{money}")
    public void recharge(@PathVariable("id") int id,@PathVariable("money") double money){
         userService.recharge(id,money);
    }
    @GetMapping("/getBookPage/{pages}")
    public PageInfo<Book> getBookPage(@PathVariable("pages") int pages){
        return userService.getBookPage(pages);
    }
}


